package com.junzhiit.nengji.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.BookingBO;
import com.junzhiit.nengji.bo.BookingListBO;
import com.junzhiit.nengji.cms.service.BookingService;
import com.junzhiit.nengji.cms.web.exception.BookingNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.BookingVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.BookingDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Booking;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.enums.BookingStatus;

@Service
public class BookingServiceImpl extends BaseServiceImpl implements BookingService{

	@Autowired
	private BookingDAO bookingDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Override
	public Map<String, Object> findPageAll(DataTablesParamVO dtp,BookingVO bookingVO){
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(bookingVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		BookingListBO bookingListBO=new BookingListBO();
		BeanUtils.copyProperties(bookingVO, bookingListBO);
		bookingListBO.setShopId(shop.getId());
		
		Date startDate=null;
		Date endDate=null;
		Date buyStartDate=null;
		Date buyEndDate=null;
		if(bookingVO.getStartDate()!=null){
			startDate=DateUtil.getDateStart(DateUtil.parseDate(bookingVO.getStartDate()));
		}
		if(bookingVO.getEndDate()!=null){
			endDate=DateUtil.getDateEnd(DateUtil.parseDate(bookingVO.getEndDate()));
		}
		if(bookingVO.getBuyStartDate()!=null){
			buyStartDate=DateUtil.getDateStart(DateUtil.parseDate(bookingVO.getBuyStartDate()));
		}
		if(bookingVO.getBuyEndDate()!=null){
			buyEndDate=DateUtil.getDateEnd(DateUtil.parseDate(bookingVO.getBuyEndDate()));
		}
		bookingListBO.setStartDate(startDate);
		bookingListBO.setEndDate(endDate);
		bookingListBO.setBuyStartDate(buyStartDate);
		bookingListBO.setBuyEndDate(buyEndDate);
		
		List<BookingBO> list = bookingDAO.findPageAll(bookingListBO, dtp.getStart(), dtp.getLength());
		int totalCount = bookingDAO.getTotalCount(bookingListBO);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO findById(String id){
		ResultVO result=new ResultVO();
		
		BookingBO data=bookingDAO.findById(id);
		if(data==null){
			throw new BookingNotFoundException("预订单未找到");
		}
		result.setData(data);
		
		return result;
	}
	
	@Override
	public ResultVO updateStatus(String id,int status){
		ResultVO result=new ResultVO();
		
		Booking data=bookingDAO.query(id);
		if(data==null){
			throw new BookingNotFoundException("预订单未找到");
		}
		
		if(BookingStatus.COMPLETE.toIntValue()==status){
			Booking booking=new Booking();
			booking.setStatus(BookingStatus.COMPLETE.toIntValue());
			booking.setId(id);
			bookingDAO.update(booking);
		}
		
		return result;
	}
	
	
	@Override
	public ResultVO getPayCount(String sessionId){
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		int count=bookingDAO.getPayCount(shop.getId());
		result.setData(count);
		
		return result;
	}
	
	@Override
	public ResultVO updateStatusByValidTime(){
		ResultVO result=new ResultVO();
		Boolean data=bookingDAO.updateStatusByValidTime();
		result.setData(data);
		return result;
	}
}
